package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.crawlers.article.ShiGuangWangParser;
import com.dmcb.trade.business.entities.Author;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 时光网所有文章解析
 * Created by Administrator on 2016/7/21.
 */
@Service
public class ShiGuangWangCrawler extends BaseCrawler {
    Logger logger = LoggerFactory.getLogger(JianshuCrawler.class);

    @Autowired
    private ShiGuangWangParser shiGuangWangParser;
    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;

    @Override
    protected String url() {
        return CrawlConstant.SHIGUANGWANG_URL;
    }

    @Override
    protected int tag() {
        return CrawlConstant.SHIGUANGWANG_TAG;
    }

    @Override
    protected String avatarSelect() {
        return "div.member_info>div>div>div>div>a>img";
    }

    @Override
    protected String channelNameSelect() {
        return "div.pl9>p>a";
    }

    @Override
    protected String listSelect() {
        return "div#blogInfoRegion";
    }

    @Override
    protected String titleSelect() {
        return "h2>b>a";
    }

    @Override
    protected String authorSelect() {
        return "div.pl9>p>a";
    }

    @Override
    protected String authorLink() {
        return null;
    }

    @Override
    protected JsonResult crawl(int authorId, String link, Author author, Document doc, boolean all) {
        // 爬取文章

        int totalAll = 0;
        int page=1;
        while (page <= CrawlConstant.CRAWL_PAGE_TOTAL) {
            JsonResult result = iterate(authorId, doc);
            page++;
            if (!result.isSuccess()) {
                return result;
            }
            totalAll += result.getInt();
            if (!all && result.getInt() < 1) {
                break;
            }
            doc = Jsoup.parse(webClientUtil.getPage("http://i.mtime.com/cydenylau/blog/index-"+page+".html").asXml(), baseUrl(link));
        }
        return totalAll == 0 ? error(CrawlConstant.MSG_WARN_NO_UPDATE) : success(totalAll);
    }





    @Override
    protected JsonResult iterate(int authorId, Document doc) {
        return super.iterate(authorId, doc);
    }

    @Override
    protected JsonResult parse(int authorId, String link) throws Exception {
        return shiGuangWangParser.parse(authorId, link);
    }

    @Override
    protected String getBody(Document doc) {
        return shiGuangWangParser.getBody(doc);
    }
}

